package com.splic.web.controller.product;

import java.util.List;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.*;
import com.splic.product.dto.GoodsTypeDto;
import com.splic.product.dto.UserSubRecordDto;
import com.splic.product.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.splic.common.annotation.Log;
import com.splic.common.enums.BusinessType;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.common.core.page.TableDataInfo;

/**
 * 用户订阅记录Controller
 * 
 * @author singe
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/product/userSubRecord")
public class UserSubRecordController extends BaseController
{
    private String prefix = "product/userSubRecord";
    @Autowired
    private UserService userServiceImpl;
    @Autowired
    private DeviceService deviceServiceImpl;
    @Autowired
    private UserSubRecordService userSubRecordServiceImpl;
    @Autowired
    private GoodsService goodsServiceImpl;
    @Autowired
    private GoodsTypeService goodsTypeServiceImpl;
    @Autowired
    private ProductService productServiceImpl;

    @RequiresPermissions("product:userSubRecord:view")
    @GetMapping()
    public String userSubRecord(ModelMap mmap)
    {
        List<Goods> goodsList = goodsServiceImpl.selectGoodsList(null);
        mmap.put("goodsList",goodsList);
//        List<GoodsType> goodsTypeList = goodsTypeServiceImpl.selectGoodsTypeList(null);
//        mmap.put("goodsTypeList",goodsTypeList);
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        return prefix + "/list";
    }

    /**
     * 查询用户订阅记录列表
     */
    @RequiresPermissions("product:userSubRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserSubRecordDto userSubRecordDto)
    {
        startPage();
        List<UserSubRecordDto> list = userSubRecordServiceImpl.selectPageList(userSubRecordDto);
        return getDataTable(list);
    }

    /**
     * 新增用户订阅记录
     */
    @GetMapping(value = { "/form/{id}", "/form" })
    public String form(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        if (StringUtils.isNotNull(id)){
            UserSubRecordDto userSubRecordDto = userSubRecordServiceImpl.selectUserSubRecordById(id);
            GoodsType goodsType = new GoodsType();
            goodsType.setProductId(userSubRecordDto.getProductId());
            List<GoodsType> goodsTypeList = goodsTypeServiceImpl.selectGoodsTypeList(goodsType);
            mmap.put("goodsTypeList",goodsTypeList);
            userSubRecordDto.setUserIdentify(userServiceImpl.selectUserById(userSubRecordDto.getUserId()).getUserIdentify());
            if (userSubRecordDto.getDeviceId() != null) {
                userSubRecordDto.setDeviceIdentify(deviceServiceImpl.selectDeviceById(userSubRecordDto.getDeviceId()).getDeviceIdentify());
            }
            mmap.put("userSubRecord", userSubRecordDto);
        }else{
            mmap.put("userSubRecord", new UserSubRecordDto());
        }
        return prefix + "/form";
    }


    /**
     * 新增保存用户订阅记录
     */
    @RequiresPermissions("product:userSubRecord:edit")
    @Log(title = "用户订阅记录", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(UserSubRecordDto userSubRecord)
    {
        if (userSubRecord.getId() == null || userSubRecord.getId() == 0) {
            if (userSubRecord.getUserId() == null || userSubRecord.getUserId() == 0) {
                User user = userServiceImpl.getByUserIdentify(userSubRecord.getUserIdentify());
                if (user != null) {
                    userSubRecord.setUserId(user.getId());
                }
            }
        }
        userSubRecord.setProductName(productServiceImpl.selectProductById(userSubRecord.getProductId()).getName());
        userSubRecord.setGoodsTypeName(goodsTypeServiceImpl.selectGoodsTypeById(userSubRecord.getGoodsTypeId()).getName());
        return toAjax(userSubRecordServiceImpl.saveOrUpdate(userSubRecord));
    }

    /**
     * 新增或修改保存其他服et务与卡群组绑定
     */
    @GetMapping("/addChangeType")
    @ResponseBody
    public List<GoodsTypeDto> addChangeType(Long productId)
    {
        GoodsType goodsType = new GoodsType();
        goodsType.setProductId(productId);
        List<GoodsTypeDto> list = goodsTypeServiceImpl.selectPageList(goodsType);
        return list;
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:userSubRecord:remove")
    @Log(title = "用户订阅记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        return toAjax(userSubRecordServiceImpl.deleteById(id));
    }


    /**
     * 导出用户订阅记录列表
     */
    @RequiresPermissions("product:userSubRecord:export")
    @Log(title = "用户订阅记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserSubRecord userSubRecord)
    {
        List<UserSubRecord> list = userSubRecordServiceImpl.selectUserSubRecordList(userSubRecord);
        ExcelUtil<UserSubRecord> util = new ExcelUtil<UserSubRecord>(UserSubRecord.class);
        return util.exportExcel(list, "用户订阅记录数据");
    }

}
